﻿using Framework.Scene;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BridgeShips {
    public class LoadSceneAgent : MonoBehaviour, ILoadSceneAgent {
        public AsyncOperation m_Operation;
        public float m_loadProcess;
        public void Init() {

        }
        public void Reset() {

        }
        public void Destroy() {

        }
        public void LoadScene( string sceneName ) {
            if (string.IsNullOrEmpty( sceneName )) return;
            UnityEngine.SceneManagement.SceneManager.LoadScene( sceneName );
        }
        public void LoadScene( int sceneId ) {
            UnityEngine.SceneManagement.SceneManager.LoadScene( sceneId );
        }
        public void LoadSceneAsync( string sceneName, LoadSceneCallback callback ) {
            StartCoroutine( LoadSceneCoro( sceneName, callback ) );
        }
        public void LoadSceneAsync( int sceneId, LoadSceneCallback callback ) {
            Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneByBuildIndex( sceneId );
            StartCoroutine( LoadSceneCoro( scene.name, callback ) );
        }

        private IEnumerator LoadSceneCoro( string sceneName, LoadSceneCallback callback ) {
            m_loadProcess = 0;
            int targetProgress = 0;
            m_Operation = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync( sceneName );
            if (callback != null) {
                if (callback.LoadSceneStart != null)
                    callback.LoadSceneStart( sceneName );
            }
            m_Operation.allowSceneActivation = false;
            while (m_Operation.progress < 0.9f) {
                targetProgress = (int)(m_Operation.progress * 100);
                while (m_loadProcess < targetProgress) {
                    m_loadProcess++;
                    if (callback != null && callback.LoadSceneUpdate != null)
                        callback.LoadSceneUpdate( sceneName, (int)(m_loadProcess) );
                    yield return null;
                }
                yield return null;
            }
            targetProgress = 100;
            while (m_loadProcess < targetProgress) {
                m_loadProcess++;
                //Debug.Log( " m_progress = " + m_loadProcess );
                if (callback != null && callback.LoadSceneUpdate != null)
                    callback.LoadSceneUpdate( sceneName, (int)(m_loadProcess) );
                yield return null;
            }
            if (callback != null && callback.LoadSceneComplete != null)
                callback.LoadSceneComplete( sceneName );
            // m_Operation.allowSceneActivation = true;
        }

        public int GetLoadingProcess() {
            if (m_loadProcess > 0.01 && m_Operation.allowSceneActivation == false) {
                return (int)(m_loadProcess);
            }
            else {
                return 0;
            }
        }


        public void AllowSceneActivation() {
            if (m_loadProcess > 99.9)
                m_Operation.allowSceneActivation = true;
        }

        public void OnUpdate( float frameTime, float realTime ) {

        }
    }
}